package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.AttendMapper;
import com.example.superoa.model.mappers.LoginMapper;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.vo.LoginVo;

//rollbackFor 当方法抛出指定异常时，进行事务回滚
@Transactional(rollbackFor=Exception.class)
@Service
public class LoginService {
	@Autowired
	private LoginMapper lm;
	@Autowired
	private StaffDao s;
	@Autowired
	private AttendMapper a_map;
	
	Shijian sj=new Shijian();
	
	public LoginVo login(String Staff_name,String Staff_pwd) {
		LoginVo opin=lm.findLoginok(Staff_name, Staff_pwd);
		return opin;
	}
	public Staff staff(int id) {
		Staff ss=s.findById(id).get();
		return ss;
	}
	public JSONArray tongji(Integer sid) {
		List<Integer> list=sj.getyearmonth(new Date(sj.getNetworkTime()));
		List<String> tian=sj.getDayByMonth(list.get(0), list.get(1)-1,list.get(2),2);
		//通过员工id查询所有考勤记录
		JSONArray kao=a_map.selectattendStaff(sid);
		Integer shang=0,zao=0,chi=0;
		BigDecimal kuang=new BigDecimal(0);
		int i=0,s=1;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		JSONArray arr=new JSONArray();
		for (String string : tian) {
			i++;
			Date day = null;
			try {
				day=sdf.parse(string);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(i==8||sj.getPanduanShi2(day, new Date(sj.getNetworkTime()))==0) {
				JSONObject jsonobje=new JSONObject();
				jsonobje.put("product", "第"+s+"周");
				jsonobje.put("上班", shang);
				jsonobje.put("迟到", chi);
				jsonobje.put("早退", zao);
				jsonobje.put("旷工", kuang);
				arr.add(jsonobje);
				i=0;
				s++;
			}
			//查看考勤记录，判断今天是否有考勤
			ok:for (Object o1 : kao) {
				JSONObject objj=(JSONObject)o1;
				if(sj.getPanduanShi2(objj.getDate("a_date"), day)==0) {
					System.out.println(objj);
					shang++;
					if(objj.getInteger("a_chidao")==1) {
						chi++;
					}
					if(objj.getInteger("a_zaotui")==1) {
						zao++;
					}
					if(objj.getBigDecimal("a_kuang").compareTo(new BigDecimal(0))>0) {
						kuang=kuang.add(objj.getBigDecimal("a_kuang"));
					}
					break ok;
				}
			}
		}
		return arr;
	}
}
